#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "exif_utils.h"

// Function to read EXIF data from a file
int read_exif_data(const char *filename, ExifData *exif_data) {
    FILE *fp = fopen(filename, "r");
    if (!fp) return -1;
    char line[128];
    while (fgets(line, sizeof(line), fp)) {
        if (strncmp(line, "Maker:", 6) == 0) {
            strncpy(exif_data->maker, line + 6, sizeof(exif_data->maker) - 1);
            exif_data->maker[sizeof(exif_data->maker) - 1] = '\0';
            char *newline = strchr(exif_data->maker, '\n');
            if (newline) *newline = '\0';
        } else if (strncmp(line, "ISO:", 4) == 0) {
            exif_data->iso = atoi(line + 4);
        }
    }
    fclose(fp);
    return 0; // Return 0 on success
}

// Function to write EXIF data to a file
int write_exif_data(const char *filename, const ExifData *exif_data) {
    FILE *fp = fopen(filename, "w");
    if (!fp) return -1;
    fprintf(fp, "Maker:%s\n", exif_data->maker);
    fprintf(fp, "ISO:%d\n", exif_data->iso);
    fclose(fp);
    return 0; // Return 0 on success
}

// Function to modify the Maker information in EXIF data
int modify_maker(ExifData *exif_data, const char *new_maker) {
    if (exif_data == NULL || new_maker == NULL) {
        return -1; // Error: invalid parameters
    }
    strncpy(exif_data->maker, new_maker, sizeof(exif_data->maker) - 1);
    exif_data->maker[sizeof(exif_data->maker) - 1] = '\0'; // Ensure null termination
    return 0; // Success
}

// Function to modify the ISO information in EXIF data
int modify_iso(ExifData *exif_data, int new_iso) {
    if (exif_data == NULL) {
        return -1; // Error: invalid parameters
    }
    exif_data->iso = new_iso;
    return 0; // Success
}